﻿Public Class ResultsTable

    Private googleresults(0 To 99) As ResultsItem
    Private bingresults(0 To 99) As ResultsItem
    Private yahooresults(0 To 99) As ResultsItem

    Public googlecount As Integer = 0
    Public bingcount As Integer = 0
    Public yahoocount As Integer = 0

    Public Sub reset()
        googlecount = 0
        bingcount = 0
        yahoocount = 0
    End Sub

    Public Sub addResult(ByVal title As String, ByVal myURL As String, ByVal source As String, ByVal desc As String)
        If source = "google" Then
            If googlecount < 100 Then
                googleresults(googlecount) = New ResultsItem
                googleresults(googlecount).setInfo(title, myURL, source, (100 - googlecount), desc)
                googleresults(googlecount).setFlag(False)
                googlecount = googlecount + 1
            End If
        ElseIf source = "bing" Then
            If bingcount < 100 Then
                bingresults(bingcount) = New ResultsItem
                bingresults(bingcount).setInfo(title, myURL, source, (100 - bingcount), desc)
                bingresults(bingcount).setFlag(False)
                bingcount = bingcount + 1
            End If
        ElseIf source = "yahoo" Then
            If yahoocount < 100 Then
                yahooresults(yahoocount) = New ResultsItem
                yahooresults(yahoocount).setInfo(title, myURL, source, (100 - yahoocount), desc)
                yahooresults(yahoocount).setFlag(False)
                yahoocount = yahoocount + 1
            End If
        End If
    End Sub

    Public Function sortAndCombine() As ResultsItem()
        yahoocount = yahoocount - 1
        bingcount = bingcount - 1
        googlecount = googlecount - 1

        Dim tocompare As String
        Dim similarcount As Integer = 0

        For googlemetritis As Integer = 0 To googlecount
            tocompare = googleresults(googlemetritis).getURL

            For bingmetritis As Integer = 0 To bingcount

                If bingresults(bingmetritis).getFlag = False Then

                    If bingresults(bingmetritis).getURL = tocompare Then
                        bingresults(bingmetritis).setFlag(True)
                        googleresults(googlemetritis).bscore = bingresults(bingmetritis).getScore
                        googleresults(googlemetritis).setScore(googleresults(googlemetritis).getScore + bingresults(bingmetritis).getScore)
                        similarcount = similarcount + 1
                        googleresults(googlemetritis).setIsBing()
                    End If

                End If

            Next

            For yahoometritis As Integer = 0 To yahoocount

                If yahooresults(yahoometritis).getFlag = False Then

                    If yahooresults(yahoometritis).getURL = tocompare Then
                        yahooresults(yahoometritis).setFlag(True)
                        googleresults(googlemetritis).yscore = yahooresults(yahoometritis).getScore
                        googleresults(googlemetritis).setScore(googleresults(googlemetritis).getScore + yahooresults(yahoometritis).getScore)
                        similarcount = similarcount + 1
                        googleresults(googlemetritis).setIsYahoo()
                    End If

                End If

            Next

        Next

        For bingmetritis2 As Integer = 0 To bingcount

            If bingresults(bingmetritis2).getFlag = False Then
                tocompare = bingresults(bingmetritis2).getURL

                For yahoometritis2 As Integer = 0 To yahoocount

                    If yahooresults(yahoometritis2).getFlag = False Then

                        If yahooresults(yahoometritis2).getURL = tocompare Then
                            yahooresults(yahoometritis2).setFlag(True)
                            bingresults(bingmetritis2).yscore = yahooresults(yahoometritis2).getScore
                            bingresults(bingmetritis2).setScore(bingresults(bingmetritis2).getScore + yahooresults(yahoometritis2).getScore)
                            similarcount = similarcount + 1
                            bingresults(bingmetritis2).setIsYahoo()
                        End If

                    End If

                Next

            End If

        Next
        Dim finishedlength As Integer = googlecount + yahoocount + bingcount - similarcount + 2
        Dim finishedtable(0 To finishedlength) As ResultsItem
        Dim finishedtablecounter As Integer = 0

        For count As Integer = 0 To (googlecount + yahoocount + bingcount)
            If count <= googlecount Then
                If googleresults(count).getFlag = False Then
                    finishedtable(finishedtablecounter) = googleresults(count)
                    finishedtablecounter = finishedtablecounter + 1
                End If

            ElseIf ((count - googlecount) <= bingcount) Then
                If bingresults(count - googlecount).getFlag = False Then
                    finishedtable(finishedtablecounter) = bingresults(count - googlecount)
                    finishedtablecounter = finishedtablecounter + 1
                End If
            Else
                If yahooresults(count - googlecount - bingcount).getFlag = False Then
                    finishedtable(finishedtablecounter) = bingresults(count - googlecount - bingcount)
                    finishedtablecounter = finishedtablecounter + 1
                End If
            End If
        Next

        Dim temp As ResultsItem
        Dim found As Integer

        For count As Integer = 0 To finishedlength
            If Not (finishedtable(count) Is Nothing) Then
                temp = finishedtable(count)
                found = count

                For count2 As Integer = (count + 1) To finishedlength
                    If Not (finishedtable(count2) Is Nothing) Then
                        If finishedtable(count2).getScore > temp.getScore Then
                            temp = finishedtable(count2)
                            found = count2
                        End If
                    End If
                Next
                finishedtable(found) = finishedtable(count)
                finishedtable(count) = temp
            End If
        Next

        Return finishedtable

    End Function
End Class

